{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4352b580",
   "metadata": {},
   "source": [
    "---------------\n",
    "* 日期：2022-10-25 \\ week08课后作业\n",
    "* 课程：python\n",
    "* 记录人：Zouyimeng\n",
    "----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7a9b5f7",
   "metadata": {},
   "source": [
    "# 总结列表的使用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0602e47",
   "metadata": {},
   "source": [
    " | 列表的方法 | 描述（Description） |使用方法 | 备注 | \n",
    "  | :----------- | :----------- |:----------- |:----------- | \n",
    "  | append | Append object to the end of the list. |list.append(object)|注意：增加在末尾！|\n",
    "  | extend | Extend list by appending elements from the iterable(可迭代对象）. |extend(iterable, /) |注意：新的内容是添加到原列表最后一个元素的后面 |\n",
    "  | insert | Insert object before index. |insert(index, object, /) |注意：添加到指定元素前面 |\n",
    "  | remove |   Remove first occurrence of value.|remove(value, /) |注意：将指定元素删除时，若列表中有多个匹配元素，则只删除第一个；若指定元素不存在，程序就会报错 |\n",
    "  | Pop | Remove and return item at index (default last)|pop(index=-1, /)  |注意：pop(a)中a表示的是元素的下标索引值 |\n",
    "  | clear | Remove all items from list.|clear()   |注意：|\n",
    "  | copy | Return a shallow copy of the list. |copy() | 备注 | \n",
    "  | count | Return number of occurrences of value. |count(value, /) | 备注 | \n",
    "  | reverse |  Reverse *IN PLACE*. |reverse()| 备注 | \n",
    "  | sort |   Sort the list in ascending order and return None. |sort(*, key=None, reverse=False) | 备注 | "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8735317e",
   "metadata": {},
   "source": [
    "## 列表的学习方法(步骤)\n",
    "> 1. 使用.Tab键查看列表的方法\n",
    "> 2. 使用help(查看方法的使用规则)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f2941d",
   "metadata": {},
   "source": [
    "* extend（可迭代对象，例如列表[]）用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f0327ead",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7920ad10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2b19d68a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cb799fba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.extend([4,5,6])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cd4510a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.extend([\"小花\",\"小草\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f943e35b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, '小花', '小草']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f08a41",
   "metadata": {},
   "source": [
    "* append(元素)用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4576d22d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "072baf1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = [\"a\",\"b\",\"c\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "655af4cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'd']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.append(\"d\")\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc2835e0",
   "metadata": {},
   "source": [
    "* 比较append和extend的用法区别："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f6df8b59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6]]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [1,2,3]\n",
    "B = [4,5,6]\n",
    "A.append(B)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "540ca979",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.index(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8742017e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "81572ddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9bffc029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [1,2,3]\n",
    "B = [4,5,6]\n",
    "A.extend(B)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "05d44619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3061e3aa",
   "metadata": {},
   "source": [
    "* insert(index, object, /)用法    # index（）函数用于从列表中找出某个值第一个匹配项的索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0c6578eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b547b7be",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ecea9dd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "A.insert(2,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e1baab8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 9, 3]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fcf542e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "A.insert(1,\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "cc3efbfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'hello', 2, 9, 3]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a69f80",
   "metadata": {},
   "source": [
    "* remove(value, /)用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0fdeee76",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "10a7116b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "d9a6a5c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [\"小花\",\"小草\",\"小河\",\"小花\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c97d452d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小草', '小河', '小花']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.remove(\"小花\")\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "bbc5ec59",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [54]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mA\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m小明\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      2\u001b[0m A\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "A.remove(\"小明\")\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21a3c56b",
   "metadata": {},
   "source": [
    "* pop(index=-1, /)用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "689284f9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0b2de2bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "5fe45f19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7029e92c",
   "metadata": {},
   "source": [
    "* clear()用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "43a916f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a2850c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0823eebf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function copy:\n",
      "\n",
      "copy() method of builtins.list instance\n",
      "    Return a shallow copy of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "42d3cb58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function count:\n",
      "\n",
      "count(value, /) method of builtins.list instance\n",
      "    Return number of occurrences of value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3fe772f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function reverse:\n",
      "\n",
      "reverse() method of builtins.list instance\n",
      "    Reverse *IN PLACE*.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.reverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f78d1dc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aeaef499",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
